<?php

/**
 * Description of TxSMS
 * @time 2022-09-03
 */

namespace globalunit\utils;

use QYS\Log\Log;

class TxSMS
{

    //put your code here
    // 参数
    private $uid = 'AKIDkLiqjNmwsZ2GFzf8NOAh0LafgV7iANPG'; //在 云API密钥 上申请的标识身份 SecretId 入口https://console.cloud.tencent.com/capi
    private $pwd = 'h8KI1VjlaAVbT7WgbIkxve8NM6bq25uV'; //在 云API密钥 上申请的标识身份 SecretKey
    private $TemplateID = '1561844'; // 模板 ID，必须填写已审核通过的模板 ID
    private $SmsSdkAppid = '1400747592'; //短信SdkAppid在 [短信控制台](https://console.cloud.tencent.com/smsv2) 添加应用后生成的实际SdkAppid，示例如1400006666。
    private $post_url = 'https://sms.tencentcloudapi.com/';


    /**
     * 参数只有一个或者没有短信发送
     * 注意点：腾讯短信接口 参数空字符不需要传递
     * 测试时，尽量有的没得都试一下
     * @param type $mobile              手机号
     * @param type $TemplateParamSet    模板信息
     * @return boolean                  成功返回boolean true
     */
    public function sendcode($mobile, $TemplateParamSet)
    {
        $Timestamp = time();
        $Nonce = mt_rand(111111, 999999);
        $Sign = '深圳兔展智能'; //短信签名内容，使用 UTF-8 编码，必须填写已审核通过的签名
        $data = array(
            'Action' => 'SendSms',
            'PhoneNumberSet.0' => '+86' . $mobile,
            'TemplateID' => $this->TemplateID,
            'Sign' => $Sign,
            'TemplateParamSet.0' => $TemplateParamSet,
            'SmsSdkAppid' => $this->SmsSdkAppid,
            'Nonce' => $Nonce,
            'Region' => 'ap-guangzhou',
            'SecretId' => $this->uid,
            'Timestamp' => $Timestamp,
            'Version' => '2019-07-11',
        );
        if (empty($TemplateParamSet)) {
            unset($data['TemplateParamSet.0']);
        }
        $qsign = $this->tosign('POST', $data, $this->pwd);
        $data['Signature'] = $qsign;
        $result = $this->_sendRequest($this->post_url, $data, 'POST');
        Log::var_dump($result);
        $result_ary = json_decode($result, true); // json转array
        Log::var_dump($result_ary);
        $req = $result_ary['Response']['SendStatusSet'][0]['Code'];
        if ($req == 'Ok') {
            // 发送成功
            return true;
        } else {
            //记录日志
            Log::var_dump(["fail" => $result_ary]);
        }
        return false;
    }

    public function _sendRequest($url, $paramArray, $method = 'POST')
    {
        $ch = curl_init();
        if ($method == 'POST') {
            $paramArray = is_array($paramArray) ? http_build_query($paramArray, null, '&', PHP_QUERY_RFC3986) : $paramArray;
            var_dump(["paramArray" => $paramArray]);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArray);
        } else {
            $url .= '?' . http_build_query($paramArray, null, '&', PHP_QUERY_RFC3986);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if (false !== strpos($url, "https")) {
            // 证书
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $resultStr = curl_exec($ch);
        return $resultStr;
    }

    public function tosign($pa, $param, $pwd)
    {
        ksort($param);
        $signStr = $pa . "sms.tencentcloudapi.com/?";
        foreach ($param as $key => $value) {
            $signStr = $signStr . $key . "=" . $value . "&";
        }
        $signStr = substr($signStr, 0, -1);
        var_dump(["signStr" => $signStr]);
        $signature = base64_encode(hash_hmac("sha1", $signStr, $pwd, true));
        return $signature;
    }
}